﻿#define _CRT_SECURE_NO_WARNINGS
#include "Snake.h"

void SetPos(int x, int y)//设置光标位置
{
	COORD pos = { x, y };
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, pos);
}

//欢迎界面
void WelcomeGame()
{
	SetPos(30, 10);
	wprintf(L"宝贝来到贪吃蛇游戏了！，<(￣︶￣)↗[GO!]");
	SetPos(34, 13);
	system("pause");

	//游戏操作介绍
	SetPos(20, 10);
	wprintf(L"宝宝！，你可以通过键盘上的箭头上下左右，来控制蛇蛇噢！q(≧▽≦q)");
	SetPos(36, 13);
	system("pause");
	system("cls");//忘记这步操作了
}

//绘制地图
void CreatMap()
{
	int i = 0;
	//上
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 26);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

void InitSnake(PSnake ps)
{
	//对蛇进行初始化
	//循环创建5个节点
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake malloc");
			return;
		}
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		cur->next = NULL;

		//使用头插来创建蛇身
		//头部
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇身体
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_FoodMark = 10;
	ps->_Score = 0;
	ps->_Dir = Right;
	ps->_SleepTime = 200;
	ps->_Status = Normal;
	//getchar();
}

//创建食物
void CreatFood(PSnake ps)
{
	int x = 0;
	int y = 0;

again:
	do
	{
		x = rand() % 53 + 2;// 2~54
		y = rand() % 25 + 1;// 1~24
	} while (x % 2 != 0);//犯了小错误 x % 2 == 0
	
	//不能与蛇身位置重和
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (food == NULL)
	{
		perror("malloc");
		return;
	}
	else
	{
		food->x = x;
		food->y = y;
		//food->next = NULL;
		SetPos(x, y);
		wprintf(L"%lc", FOOD);
		ps->_pFood = food;
	}
	//getchar();
}

//游戏开始
void GameStart(PSnake ps)
{
	//0 设置窗口大小
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");

	//1 隐藏光标
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOutput, &CursorInfo);
	//2 打印欢迎界面
	WelcomeGame();
	//3 绘制地图
	CreatMap();
	//4 创建蛇
	InitSnake(ps);
	//5 创建食物
	CreatFood(ps);
}

//实现游戏逻辑
#define KEY_PRESS(VK) ((GetAsyncKeyState(VK) & 1) ? 1 : 0)

//打印游戏分数变化，以及其余自己加入的信息
void PrintInfo(PSnake ps)
{
	SetPos(60, 15);
	printf("总分数 =  %d", ps->_Score);
	SetPos(60, 16);
	printf("当前每个食物分数 =  %2d", ps->_FoodMark);
}

void PrintHelpInfo()
{
	SetPos(60, 18);
	printf("不能撞墙，否则趋势！！！！！！！！！！");

}

//暂停
void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

//吃掉食物
void EetFood(pSnakeNode psn, PSnake ps)
{
	//使用头插将食物设为新的头
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;

	//打印蛇身
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur-> x, cur-> y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_Score += ps->_FoodMark;
	free(ps->_pFood);
	ps->_pFood = NULL;

	//新食物
	CreatFood(ps);
}

//没有食物
void NoFood(pSnakeNode psn, PSnake ps)
{
	//使用头插将食物设为新的头
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;

	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	free(cur->next);
	cur->next = NULL;

}

//撞墙
int KillWall(PSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 || ps->_pSnake->y == 0 || ps->_pSnake->y == 26)
	{
		ps->_Status = Kill_Wall;
		return 1;
	}
	return 0;
}

//不撞墙
int KillSelf(PSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (ps->_pSnake->x == cur->x && ps->_pSnake->y == cur->y)
		{
			ps->_Status = Kill_Self;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}

//蛇的移动
void SnakeMove(PSnake ps)
{
	//我们说建立下一个新节点作为新的头打印，最后一个节点释放
	pSnakeNode node = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (node == NULL)
	{
		perror("SnakeMove():: malloc");
		return;
	}

	//确定坐标，新蛇头位置
	switch (ps->_Dir)
	{
	case Up:
	{
		node->x = ps->_pSnake->x;
		node->y = ps->_pSnake->y - 1;
	}
		break;
	case Down:
	{
		node->x = ps->_pSnake->x;
		node->y = ps->_pSnake->y + 1;
	}
		break;
	case Left:
	{
		node->x = ps->_pSnake->x - 2;
		node->y = ps->_pSnake->y;
	}
		break;
	case Right:
	{
		node->x = ps->_pSnake->x + 2;
		node->y = ps->_pSnake->y;
	}
		break;
	default:
		break;
	}
	
	if (node->x == ps->_pFood->x && node->y == ps->_pFood->y)
	{
		//吃掉食物
		EetFood(node, ps);
	}
	else
	{
		//没有食物
		NoFood(node, ps);
	}

	//撞墙
	KillWall(ps);

	//不撞墙
	KillSelf(ps);
}


void Game(PSnake ps)
{
	//打印游戏分数变化，以及其余自己加入的信息
	PrintHelpInfo();

	do
	{
		PrintInfo(ps);

		//判断设置按键
		if (KEY_PRESS(VK_UP) && ps->_Dir != Down)
		{
			ps->_Dir = Up;
		}
		else if(KEY_PRESS(VK_DOWN) && ps->_Dir != Up)
		{
			ps->_Dir = Down;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != Right)
		{
			ps->_Dir = Left;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != Left)
		{
			ps->_Dir = Right;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//游戏暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Status = GameOver;
			break;
			//退出游戏
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_SleepTime > 30)
			{
				ps->_SleepTime -= 20;
				ps->_FoodMark += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_FoodMark > 3)
			{
				ps->_SleepTime += 20;
				ps->_FoodMark -= 2;
			}
		}
		//蛇的行走
		SnakeMove(ps);
		Sleep(ps->_SleepTime);
	} while (ps->_Status == Normal);
	SetPos(0, 27);
}

//游戏结束
void GameExit(PSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	pSnakeNode rev = ps->_pSnake;
	SetPos(8, 10);
	switch (ps->_Status)
	{
	case GameOver:
		printf("游戏结束!\n");
		break;
	case Kill_Self:
		printf("您在吃自己！哇呜！游戏结束!\n");
		break;
	case Kill_Wall:
		printf("您撞墙，头头不是钢铁这般坚硬哦~！！！,游戏结束!\n");
		break;
	}
	
	//释放节点
	while (cur)
	{
		rev = cur;
		cur = cur->next;
		free(rev);
	}

}
